﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using Model;
using System.Data;

namespace DAL
{
    public class wenjuanDAO
    {
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Id","T_wenjuan");        
        }

        public bool Exists(int Id)
        {
            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("select count(1) from WeiXinRequest");
            //strSql.Append(" where ID=@ID ");
            //SqlParameter[] parameters = {
            //        new SqlParameter("@ID", SqlDbType.Int,4)};
            //parameters[0].Value = ID;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_wenjuan");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = { new SqlParameter("@Id",Id)};
            //parameters[0].Value = Id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        public int Add(wenjuanClass wjmodel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_wenjuan(");
            strSql.Append("UserId,AnswerTime,Ans1,Ans2,Ans3,Ans4,Ans5,Ans6,Ans7,Ans8,Anscount,Name,Phone,Addr,WeiXinName)");
            strSql.Append(" values (");
            strSql.Append("@UserId,@AnswerTime,@Ans1,@Ans2,@Ans3,@Ans4,@Ans5,@Ans6,@Ans7,@Ans8,@Anscount,@Name,@Phone,@Addr,@WeiXinName)");
            //strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", wjmodel.UserId),
					new SqlParameter("@AnswerTime",wjmodel.AnswerTime),
					new SqlParameter("@Ans1", wjmodel.Ans1),
					new SqlParameter("@Ans2", wjmodel.Ans2),
					new SqlParameter("@Ans3", wjmodel.Ans3),
					new SqlParameter("@Ans4", wjmodel.Ans4),
					new SqlParameter("@Ans5",wjmodel.Ans5),
					new SqlParameter("@Ans6", wjmodel.Ans6),
					new SqlParameter("@Ans7", wjmodel.Ans7),
					new SqlParameter("@Ans8", wjmodel.Ans8),
                    new SqlParameter("@Name", wjmodel.Name),
                     new SqlParameter("@Phone", wjmodel.Phone),
                     new SqlParameter("@Addr", wjmodel.Addr),
                    new SqlParameter("@Anscount", wjmodel.Anscount),
                    new SqlParameter("@WeiXinName", wjmodel.WeiXinName)
					};

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        public int Update(wenjuanClass wjmodel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Update T_wenjuan set  ");
            strSql.Append("WeiXinName=@WeiXinName  ");           
            strSql.Append(" where UserId=@UserId ");
            //strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {				
                    new SqlParameter("@UserId", wjmodel.UserId),
                    new SqlParameter("@WeiXinName", wjmodel.WeiXinName)
					};

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from  T_wenjuan ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where  " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from  T_wenjuan ");
          
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList7()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from  v_Ans7 order by 回复时间 desc ");

            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList8()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from  v_Ans8 order by 回复时间 desc");

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// sqldatareader
        /// </summary>
        /// <returns></returns>
        public SqlDataReader GetData()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from  T_wenjuan ");

            return DbHelperSQL.ExecuteReader(strSql.ToString());
        
        }

        /// <summary>
        /// 根据微信号得到数据
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>

        public int GetByFromUserName(string userid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from T_wenjuan");
            strSql.Append(" where UserId=@FromUserName");
            SqlParameter[] parameters ={
					new SqlParameter("@FromUserName", SqlDbType.VarChar,50)};
            parameters[0].Value = userid;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            //DataTable dt = new DataTable();
            return ds.Tables[0].Rows.Count;
        }


        public DataSet DataBind()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Name as 回复人,Ans7 as 回复内容, Ans8 as 回复内容  from T_wenjuan ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            return ds;
        }
    



        public bool Exists(string UserId)
        {
            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("select count(1) from WeiXinRequest");
            //strSql.Append(" where ID=@ID ");
            //SqlParameter[] parameters = {
            //        new SqlParameter("@ID", SqlDbType.Int,4)};
            //parameters[0].Value = ID;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  count(1)  from T_wenjuan");
            strSql.Append(" where  UserId=@UserId");
            SqlParameter[] parameters={ new SqlParameter("@UserId", UserId) };
            //parameters[0].Value = Id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        //public DataSet  GetPage(int size, int index,ref int pagecount)
        //{
        //    return DbHelperSQL.GetPage(size, index,ref pagecount);
        //}



     

    }
}
